package com.hndzyu.yd.task;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.Share;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.OkHttpDownloadJsonUtil;

import java.util.List;

/**
 * 分享
 * Created by zz9527 on 2018/10/19.
 */
public class TabShareFragment extends Fragment {
    private ListView lv;
    private SmartRefreshLayout refresh;
    private ImageView nodata;
    private ImageView banner;

    public static TabShareFragment newInstance(int level) {

        Bundle args = new Bundle();
        args.putInt("level", level);
        TabShareFragment fragment = new TabShareFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tab_share, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        getData(0);
    }

    private void initView(final View view) {
        final ImageView iv_sharefinish = view.findViewById(R.id.iv_sharefinish);
        final ImageView iv_share = view.findViewById(R.id.iv_share);
        final TextView tv_sharefinish = view.findViewById(R.id.tv_sharefinish);
        final TextView tv_share = view.findViewById(R.id.tv_share);
        nodata = view.findViewById(R.id.nodata);
        lv = view.findViewById(R.id.lv_share);
        refresh = view.findViewById(R.id.refresh);
        //设置 Header 样式
        refresh.setRefreshHeader(new MaterialHeader(mContext));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refresh.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                iv_share.setImageResource(R.mipmap.kcy_s);
                iv_sharefinish.setImageResource(R.mipmap.jxz);
                tv_sharefinish.setTextColor(getResources().getColor(R.color.tv_selector2));
                tv_share.setTextColor(getResources().getColor(R.color.tv_selector));
                getData(0);
            }
        });
        banner = view.findViewById(R.id.iv_share_banner);
        view.findViewById(R.id.rl_fragment_ge_task_list).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iv_share.setImageResource(R.mipmap.kcy_s);
                iv_sharefinish.setImageResource(R.mipmap.jxz);
                tv_sharefinish.setTextColor(getResources().getColor(R.color.tv_selector2));
                tv_share.setTextColor(getResources().getColor(R.color.tv_selector));
                getData(0);
            }
        });
        view.findViewById(R.id.rl_sharefinish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iv_share.setImageResource(R.mipmap.kcy);
                iv_sharefinish.setImageResource(R.mipmap.jxz_s);
                tv_sharefinish.setTextColor(getResources().getColor(R.color.tv_selector));
                tv_share.setTextColor(getResources().getColor(R.color.tv_selector2));
                getData(1);
            }
        });
        view.findViewById(R.id.ll_share).setSelected(true);
    }

    private void getData(final int type) {
        //type 0 可分享 1 已分享
        OkHttpDownloadJsonUtil.downloadJson(mContext, Path.SHARE(UserBean.uid + "", type), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try{
                    Gson gson = new Gson();
                    final Share share = gson.fromJson(json, Share.class);
                    final List<Share.DataBean> list = share.getData();
                    lv.setEmptyView(nodata);//设置当ListView为空的时候显示
                    lv.setAdapter(new ShareAdapter(list, mContext,type));
                    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            Intent intent = new Intent(getActivity(), WebActivity.class);
                            intent.putExtra("url", list.get(i).getUrl());
                            intent.putExtra("shid", list.get(i).getShid());
                            intent.putExtra("fen", list.get(i).getFen());
                            intent.putExtra("title", list.get(i).getShname());
                            intent.putExtra("img", list.get(i).getImg());
                            intent.putExtra("type", list.get(i).getType());
                            intent.putExtra("intro", list.get(i).getShcon());
                            intent.putExtra("isshow", list.get(i).getIsshow());
                            intent.putExtra("scon", list.get(i).getShowtext());
                            intent.putExtra("ym", list.get(i).getYuming());
                            getActivity().startActivity(intent);
                        }
                    });

                    Glide.with(mContext).load(Path.img(share.getBanner().getBanner())).apply(new RequestOptions().error(R.mipmap.banner_share).diskCacheStrategy(DiskCacheStrategy.NONE)).into(banner);
                    banner.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(getActivity(), WebActivity.class);
                            intent.putExtra("url", share.getBanner().getUrl());
                            intent.putExtra("shid", share.getBanner().getShid());
                            intent.putExtra("fen", share.getBanner().getFen());
                            intent.putExtra("title", share.getBanner().getShname());
                            intent.putExtra("img", share.getBanner().getBanner());
                            intent.putExtra("type", share.getBanner().getType());
                            intent.putExtra("intro", share.getBanner().getShcon());
                            intent.putExtra("isshow", share.getBanner().getIsshow());
                            intent.putExtra("scon", share.getBanner().getShowtext());
                            intent.putExtra("ym",share.getBanner().getYuming());
                            getActivity().startActivity(intent);

                        }
                    });
                    }catch (Exception e){
                        Log.i("________",e.toString());
                        Toast.makeText(mContext, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(mContext, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });

//        final List<Share.DataBean> list = new ArrayList<>();
//        for (int i=0;i<6;i++){
//            Share.DataBean dataBean = new Share.DataBean();
//            dataBean.setImg("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1540533172&di=b03fd4ec395906a154f6be0d3714f2cc&imgtype=jpg&er=1&src=http%3A%2F%2Farticles.csdn.net%2Fuploads%2Fallimg%2F120315%2F119_120315075001_1.jpg");
//            dataBean.setUrl("https://mp.weixin.qq.com/s/GBJIQ4XyhRBSXSw2r1IO7w");
//            dataBean.setShname("程序员的日常——循环");
//            dataBean.setAll("1.0");
//            dataBean.setOne("0.01");
//            list.add(dataBean);
//        }
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext= activity;
    }
    private Context mContext;

}
